package com.jisf.driver.controller;

import com.jisf.driver.entity.vo.RoleAddRequestVo;
import com.jisf.driver.entity.vo.RoleListVo;
import com.jisf.driver.response.IErrorCode;
import com.jisf.driver.response.Result;
import com.jisf.driver.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author jisf
 * @since 2022-04-08
 */
@RestController
@RequestMapping("/driver/role")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    @GetMapping("list")
    @PreAuthorize("hasAnyAuthority('role:list')")
    public Result<?> roleList(){
        List<RoleListVo> roleListVos = iRoleService.roleList();
        return Result.ok(roleListVos);
    }

    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('role:add')")
    public Result<?> roleAdd(@RequestBody RoleAddRequestVo roleAddRequestVo){
        IErrorCode iErrorCode = iRoleService.roleAdd(roleAddRequestVo);
        return Result.ok(iErrorCode);
    }

    @GetMapping("roleAll")
    @PreAuthorize("hasAnyAuthority('role:roleAll')")
    public Result<?> roleAll(){
        List<RoleListVo> allRoles = iRoleService.roleAll();
        return Result.ok(allRoles);
    }

    @GetMapping("change/status")
    @PreAuthorize("hasAnyAuthority('role:change:status')")
    public Result<?> roleChangeStatus(@RequestParam Integer roleId,
                                      @RequestParam String status){
        IErrorCode iErrorCode = iRoleService.roleChangeStatus(roleId, status);
        return Result.ok(iErrorCode);
    }

    @GetMapping("delete")
    @PreAuthorize("hasAnyAuthority('role:delete')")
    public Result<?> roleDelete(@RequestParam Integer roleId){
        IErrorCode iErrorCode = iRoleService.roleDelete(roleId);
        return Result.ok(iErrorCode);
    }

    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('role:update')")
    public Result<?> roleUpdate(@RequestBody RoleListVo roleListVo){
        IErrorCode iErrorCode = iRoleService.roleUpdate(roleListVo);
        return Result.ok(iErrorCode);
    }



}
